﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Net;
using System.Net.Sockets;

namespace peonwar
{
    class UserOnline : User
    {
        public bool IsHost;
        private Network Net;

        public UserOnline(bool ishost, bool team, string n, Network net, Game g)
            : base(team, n, g)
        {
            this.IsHost = ishost;
            this.Net = net;
        }

        public override void SpawnUnit(Unit peonCreated)
        {
            Net.Send(RequestType.Game_SpawnUnit, new UnitInfos(peonCreated));
            if (IsHost)
                Net.SyncDelay = Net.SyncDelay + 10;
            base.SpawnUnit(peonCreated);
        }

        public override void UpgradeBaseArmor()
        {
            base.UpgradeBaseArmor();
            Net.Send(RequestType.Game_BaseUpgrade, new BaseInfos(this));
        }

        public override void LaunchPower(Power power, User enemyplayer, int seed)
        {
            Tuple<Power,int> sending = new Tuple<Power,int>(power, seed);
            switch (power)
            {
                case Power.EnergyDrainer:
                    Net.Send(RequestType.Game_Power, sending);
                    Power_Drain(enemyplayer); 
                    break;
                case Power.Heal:
                    Power_Heal(); 
                    break;
                case Power.Bombe:
                    Net.Send(RequestType.Game_Power, sending);
                    Power_Bomb(enemyplayer, seed); 
                    break;
                case Power.Cresus:
                    Net.Send(RequestType.Game_Power, sending);
                    Power_Cresus(); 
                    break;
                case Power.SuperPeon:
                    Power_SuperPeon(); 
                    break;
            }
        }

        public override void Kill(User playerenemy)
        {
            Net.Send(RequestType.Game_KillUnit, new Object());
            base.Kill(playerenemy);
        }

        public override void Power_Heal()
        {
            foreach (Unit u in PeonList)
            {
                if (u.UnitType != 10)
                {
                    if (u.Health < u.MaxHealth)
                    {
                        if (u.Health < 0.10 * u.MaxHealth && Type == PlayerType.Player)
                            AchievementManager.AchievementList[15].Add();
                        u.Health = u.MaxHealth;
                        Net.Send(RequestType.Game_UpdateUnit, u.GetModel());
                        User.AdvertList.Add(new Advert(new Vector2(u.Position.X - 5, u.Position.Y - 45), GameData.Font_Pause, "+", 1.3f, new Vector2(0, -1), 1.8f, u.TeamColor, true, false));
                        User.AdvertList.Add(new Advert(new Vector2(u.Position.X - 13, u.Position.Y - 50), GameData.Font_Menu, "+", 1.5f, new Vector2(0, -1), 1.0f, u.TeamColor, true, false));
                        User.AdvertList.Add(new Advert(new Vector2(u.Position.X + 3, u.Position.Y - 45), GameData.Font_Pause, "+", 1.2f, new Vector2(0, -1), 1.5f, u.TeamColor, true, false));
                        User.AdvertList.Add(new Advert(new Vector2(u.Position.X, u.Position.Y - 40), GameData.Font_Menu, "+", 1f, new Vector2(0, -1), 1.6f, u.TeamColor, true, false));
                    }
                }
            }
            GameSound.SoundPouvoirs[0].Play(0.35f, 0, 0);
        }

        public override void UpgradeProductionLevel()
        {
            Tools.BankManage(this, -(int)(ProductionLevel * ProductionLevel * (character == Perso.Frohman ? 100 : 200)));
            ProductionLevel++;
            for (int i = 0; i < UnitCost.Length; i++)
            {
                UnitCost[i] -= (5 * (int)(1 + i / 3));
                UnitCost[i] = UnitCost[i] >= 0 ? UnitCost[i] : 0;
            }
            GameSound.baseUpSound.Play();
            Net.Send(RequestType.Game_ProductionLevel, new Object());
        }
    }
}
